当前位置: 代码迷 >> Java Exception >> jsp 表单提交乱码
  详细解决方案

jsp 表单提交乱码

热度:0   发布时间:2025-07-05 17:43:45.0

JSP表单提交乱码解决方案

问题原因分析

JSP表单提交出现乱码通常是由于字符编码设置不正确导致的,主要有以下几种情况:

  • 页面编码与服务器接收编码不一致
  • GET和POST请求处理方式不同
  • 数据库连接未设置正确编码

完整解决方案

1. 设置JSP页面编码

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

2. 设置HTML表单编码

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<form action="process.jsp" method="post" accept-charset="UTF-8">

3. 处理POST请求乱码

在接收参数的JSP页面或Servlet中添加:

<%
request.setCharacterEncoding("UTF-8");
%>

4. 处理GET请求乱码

对于GET请求,需要在服务器配置中设置URI编码:

在Tomcat的server.xml中找到Connector标签,添加:

<Connector URIEncoding="UTF-8" ... />

5. 完整示例代码

表单页面(index.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>表单示例</title>
</head>
<body>
  <form action="process.jsp" method="post">
    用户名: <input type="text" name="username"><br>
    留言: <textarea name="message"></textarea><br>
    <input type="submit" value="提交">
  </form>
</body>
</html>

处理页面(process.jsp)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
// 设置请求编码
request.setCharacterEncoding("UTF-8");

// 获取参数
String username = request.getParameter("username");
String message = request.getParameter("message");

// 处理数据...
%>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>处理结果</title>
</head>
<body>
  <h2>提交结果</h2>
  <p>用户名: <%= username %></p>
  <p>留言: <%= message %></p>
</body>
</html>

6. 使用过滤器统一处理编码

创建编码过滤器(EncodingFilter.java):

package com.example.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class EncodingFilter implements Filter {
  private String encoding = "UTF-8";
  
  public void init(FilterConfig config) throws ServletException {
    String encodingParam = config.getInitParameter("encoding");
    if (encodingParam != null) {
      encoding = encodingParam;
    }
  }
  
  public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException, ServletException {
    request.setCharacterEncoding(encoding);
    response.setCharacterEncoding(encoding);
    chain.doFilter(request, response);
  }
  
  public void destroy() {
    // 清理资源
  }
}

在web.xml中配置过滤器:

<filter>
  <filter-name>encodingFilter</filter-name>
  <filter-class>com.example.filter.EncodingFilter</filter-class>
  <init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
  </init-param>
</filter>
<filter-mapping>
  <filter-name>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

注意事项

  • 确保所有环节(页面、请求处理、数据库)都使用相同的编码(推荐UTF-8)
  • GET和POST请求需要分别处理
  • 如果使用数据库,确保连接字符串也设置了正确的编码
  • 对于Tomcat 8及以上版本,默认URI编码已经是UTF-8

数据库连接编码设置示例

MySQL连接示例:

jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=UTF-8
  相关解决方案